<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
    }

    body {
      height: 2000px;
      display: flex;
      flex-direction: column;
    }

    .top {
      height: 200px;
      text-align: center;
      overflow: hidden;
    }

    button {
      padding: 10px 20px;
      font-size: 30px;
      cursor: pointer;
    }

    .content {
      flex: 1;
      background-color: orange;
      display: flex;
    }

    .content .left {
      flex: 1;
      background-color: skyblue;
      padding: 10px;
      box-sizing: border-box;
    }

    .content .item {
      display: inline-block;
      height: 30px;
      border: 1px solid #000;
      padding: 4px;
      line-height: 30px;
      border-radius: 10px;
      transition: 0.3s;
      cursor: pointer;
      margin-right: 10px;
    }

    .content .item:hover {
      box-shadow: 0 0 10px gray;
      transform: translateY(-4px);
      color: white;
      font-weight: 900;
    }

    .content .right {
      flex: 1;
      background-color: pink;
    }
  </style>
  <script src="../client/js/node_modules/axios/dist/axios.js"></script>
</head>

<body>
  <div class="top">
    <h2>抽奖</h2>
    <button>开始</button>
  </div>
  <div class="content">
    <div class="left">
      <div class="item">
        某同学-0次
      </div>
    </div>
    <div class="right"></div>
  </div>
  <script src="./lib/echarts.min.js"></script>
  <script>

    // 生成图形
    var myChart = echarts.init(document.querySelector('.right'))
    const option = {
      xAxis: {
        type: 'value'
      },
      yAxis: {
        type: 'category',
        data: ['小黑', '小红', '小绿', '小花'],
        inverse: true
      },
      tooltip: {},
      series: [
        {
          realtimeSort: true,
          data: [2, 3, 8, 5],
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          }
        }
      ]
    }
    myChart.setOption(option)

    //获取左边列表
    const left = document.querySelector(".content .left")
    //设置基地址
    axios.defaults.baseURL = 'http://127.0.0.1'
    //获取数据并渲染左边
    function leftRender() {
      axios.get('/api/students').then(res => {
        const data = res.data.map(el => {
          const { id, name, count } = el
          return `
        <div class="item" data-index='$[id]'>
          ${name}-${count}次
        </div>
        `
        })
        left.innerHTML = data.join('')
        // console.log(res)
      })
    }
    leftRender()
    //下方是点击随机抽取功能
    const button = document.querySelector('.top button')
    button.addEventListener('click', () => {
      axios.get('/api/lucystar').then(res => {
        // console.log(res)
        const lucy = res.data.name
        alert('随机抽取成功,抽到 ' + lucy + ' 同学')
        leftRender()
        rightRender()
      }).catch(err => {
        alert('抽取失败！')
      })

    })
    //右侧渲染功能
    function rightRender() {
      axios.get('/api/students').then(res => {
        const namedata = res.data.map(el => el.name)
        const countdata = res.data.map(el => el.count)
        option.yAxis.data = namedata
        option.series[0].data = countdata
        //下面注释的是无用代码
        // const data = res.data.filter(el => {
        //   return el.count > 0
        // })
        // console.log(namedata)
        // console.log(countdata)
        //重新调用
        myChart.setOption(option)
      })
    }
    rightRender()
  </script>
</body>

</html>